package org.codingq92.frame.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.HashMap;

/**
 * KafkaProducer
 *
 * @author: CodingQ92
 * @description: KafkaProducer
 */
public class KafkaProducerTest {
    public static void main(String[] args) {
        HashMap<String, Object> configMap = new HashMap<>();
        // kafka服务器地址
        configMap.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
        // 序列化操作
        configMap.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        configMap.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        try (KafkaProducer<String, Object> producer = new KafkaProducer<>(configMap)) {
            ProducerRecord<String, Object> record = new ProducerRecord<>("first", "hello kafka");
            producer.send(record);
            System.out.println("发送成功");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
